<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
use tcpdf\tcpdf;
class Orders extends Base{
    public function orders_list(){
        $keyword=input('get.keyword');
        $order_type=input('get.order_type');
        $state=input('get.state');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['a.time', '>', $fromtime],
            ['a.time', '<', $totime]
        ];
        if($state==0){
            array_push($map,['a.state','<',7]);
        }else{
            array_push($map,['a.state','=',$state]);
        }
        if($order_type==2){
            array_push($map,['a.order_type','=',$order_type]);
            $list=Db::name('ordersub')->alias('a')->join('orders b', 'a.orders_id = b.id','LEFT')->join('scenic_spot c', 'a.p_id = c.id','LEFT')->field('a.*,b.name,b.phone,c.title')->where($map)->where('a.ordernosub|b.phone|b.name|c.title','like',"%{$keyword}%")->order('a.id desc')->paginate(10);
            $count=Db::name('ordersub')->alias('a')->join('orders b', 'a.orders_id = b.id','LEFT')->join('scenic_spot c', 'a.p_id = c.id','LEFT')->field('a.*,b.name,b.phone,c.title')->where($map)->where('a.ordernosub|b.phone|b.name|c.title','like',"%{$keyword}%")->count();
        }else{
            array_push($map,['a.order_type','=',1]);
            $list=Db::name('ordersub')->alias('a')->join('orders b', 'a.orders_id = b.id','LEFT')->join('product c', 'a.p_id = c.id','LEFT')->field('a.*,b.name,b.phone,c.title')->where($map)->where('a.ordernosub|b.phone|b.name|c.title','like',"%{$keyword}%")->order('a.id desc')->paginate(10);
            $count=Db::name('ordersub')->alias('a')->join('orders b', 'a.orders_id = b.id','LEFT')->join('product c', 'a.p_id = c.id','LEFT')->field('a.*,b.name,b.phone,c.title')->where($map)->where('a.ordernosub|b.phone|b.name|c.title','like',"%{$keyword}%")->count();
        }
        
        $this->assign('keyword',input('get.keyword'));
        $this->assign('order_type',$order_type);
        $this->assign('state',$state);
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('count',$count);
        return $this->fetch();
    }
    public function orders_detail(){
        if(request()->isGet()){
            $param = input('get.');
            if($param['order_type']==1){
                $data=Db::name('ordersub')->alias('a')->join('product b', 'a.p_id = b.id','LEFT')->join('orders c', 'a.orders_id = c.id','LEFT')->field('a.*,b.img,c.name,c.phone,c.address')->where('a.id',$param['id'])->find();
            }elseif($param['order_type']==2){
                $data=Db::name('ordersub')->alias('a')->join('scenic_spot b', 'a.p_id = b.id','LEFT')->join('orders c', 'a.orders_id = c.id','LEFT')->field('a.*,b.img,c.name,c.phone,c.address')->where('a.id',$param['id'])->find();
            }
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['express_company']=$param['express_company'];
            $data['express_no']=$param['express_no'];
            $data['state']=3;
            $data['fhtime']=time();
            $data['uptime']=time();
            $orders_id=Db::name('ordersub')->where(array('id'=>$param['id']))->value('orders_id');
            Db::name('ordersub')->where(array('id'=>$param['id']))->update($data);
            $exit=Db::name('ordersub')->where(array('orders_id'=>$orders_id))->find();
            if($exit){
                return json(['code' => 200, 'msg' => '成功！']);
            }else{
                $res=Db::name('orders')->where(array('id'=>$orders_id))->update($data);
                if($res){
                        return json(['code' => 200, 'msg' => '成功！']);
                }else{
                    return json(['code' => 500, 'msg' => '失败！']);
                }
            }
            
        }
        
    }
    
}
